package org.example.java.classloader;

import java.lang.reflect.Method;
import java.nio.file.Paths;

/**
 * Juntao.Zhang on 2014/5/13.
 */
public class FileSystemClassLoaderTest {
  public static void main(String[] args) {
    String classDir = "JavaSE\\target\\classes";

      /*
     * 两个类加载器分别去加载指定的类，但这个类只能被加载一次
		 */
    FileSystemClassLoader fsc1 = new FileSystemClassLoader(Paths.get(classDir));
    FileSystemClassLoader fsc2 = new FileSystemClassLoader(Paths.get(classDir));
    final String className = "org.example.java.classloader.Sample";
    System.out.println(fsc1.classNameToPath(className));
    try {
      Class<?> class1 = fsc1.findClass(className);
      Object obj1 = class1.newInstance();
      Class<?> class2 = fsc2.findClass(className);
      Object obj2 = class2.newInstance();
      System.out.println(class1 == class2);
      System.out.println(obj1 == obj2);

      Method method1 = class1.getMethod("setSample", Object.class);
      method1.invoke(obj1, obj2);

      Class c1 = Class.forName(className);
      Class c2 = Class.forName(className);
      System.out.println(c1 == c2);

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
